﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System.Collections.Generic;
	using Falcon.Core;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Forums.Domain.Repositories;
	using Falcon.Core.Validation;

	public class ForumPostUpdate : ValueBase<ForumPostUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostUpdate"/> class.
		/// </summary>
		public ForumPostUpdate()
		{
			this.ForumPostAttachmentRepository = Application.Resolve<IForumPostAttachmentRepository>();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostUpdate"/> class.
		/// </summary>
		/// <param name="forum">The forum.</param>
		/// <param name="post">The post.</param>
		public ForumPostUpdate(Forum forum, ForumPost post) : this()
		{
			this.Id = post.Id;
			this.Title = post.Title;
			this.Body = post.Body;
			this.Forum = forum;
		}

		/// <summary>
		/// Initializes this instance.
		/// </summary>
		public void Init()
		{
			var identity = User.Identity;

			if (identity == null)
			{
				return;
			}

			this.Attachment = new AttachmentCreate
			{
				Bind = ForumsSettings.Posts.AttachmentFieldKey,
				Group = ForumsSettings.Posts.AttachmentKey,
				TempId = identity.Id,
				UserId = identity.Id
			};

			this.Attachments = this.ForumPostAttachmentRepository.GetAll(new ForumPostAttachmentSpecification
			{
				PostId = new[] { this.Id }
			});
		}

		/// <summary>
		/// Gets or sets the forum post attachment repository.
		/// </summary>
		/// <value>The forum post attachment repository.</value>
		public IForumPostAttachmentRepository ForumPostAttachmentRepository
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the attachment.
		/// </summary>
		/// <value>The attachment.</value>
		public AttachmentCreate Attachment
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the forum.
		/// </summary>
		/// <value>The forum.</value>
		public Forum Forum
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the attachments.
		/// </summary>
		/// <value>The attachments.</value>
		public IList<ForumPostAttachment> Attachments
		{
			get;
			set;
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<ForumPostUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Message));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}